<?php
/**
 * Copyright (c) 2007, lostcode.org
 * All rights reserved.
 *
 * Filename : poe.php
 * Summary  : PHP port for Perl's POE modules
 * Author   : Achilles Xu<formalin14@gmail.com>
 * Verion   : 0.1
 */
$poe_kernel_queue = array();
$poe_kernel_sidm = 1;
$poe_kernel_sessions = array();

class POE_Session {
	var $heap;
	var $args;
	var $inline_states;
	var $ID;
	
	function POE_Session($inline_states, $args = NULL) {
		global $poe_kernel_queue, $poe_kernel_sidm, $poe_kernel_sessions;
		$this->inline_states = $inline_states;
		$this->args = $args;
		$this->heap = array();
		$this->ID = $poe_kernel_sidm; 
		$poe_kernel_sessions[$poe_kernel_sidm] = &$this;
		$poe_kernel_sidm++;
		if ($inline_states['_start']) {
			$poe_kernel_queue[] = array('sid' => $this->ID, 'name'=>'_start', 'args'=>$this->args);
		}
	}
	
	function event($name, $args = NULL) {
		$func = $this->inline_states[$name];
		if ($func) {
			$func(&$this, &$this->heap, $args);
		}
	}
	
	function yield($name, $args = NULL) {
		poe_kernel_post($this->ID, $name, $args);
	}
}

function poe_kernel_run() {
	global $poe_kernel_queue, $poe_kernel_sessions;
	$rest = true;
	while ($item = &array_shift($poe_kernel_queue)) {
		$session = &$poe_kernel_sessions[$item['sid']];
		if ($session) {
			$session->event($item['name'], $item['args']);
		}
	}
}

function poe_kernel_post($sid, $name, $args = NULL) {
	global $poe_kernel_queue;
	$poe_kernel_queue[] = array('sid'=>$sid,'name'=>$name, 'args'=>$args);
}


?>
